/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "systemrebootdialog.h"

#include <QWidget>
#include <QLabel>
//#include <DWarningButton>
#include <QPushButton>

#include <QTimer>
#include <QPushButton>

#define BTN_CANCEL 0 // 弹框第一个按钮
#define BTN_REBOOT 1 // 弹框第二个按钮

static const int TIMEOUT_COUNT = 5;
SystemRebootDialog::SystemRebootDialog(DDialog *parent)
    : DDialog(parent)
    , m_timer(nullptr)
    , m_elapsedSeconds(TIMEOUT_COUNT)
{
    // 初始化界面
    initUi();
}

// 初始化界面
void SystemRebootDialog::initUi()
{
    setMinimumSize(QSize(300, 150));
    // 弹框样式
    setCloseButtonVisible(true);
    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    this->setModal(true);

    // 清除布局
    clearContents();
    clearButtons();
    setIcon(QIcon::fromTheme("deepin-defender"));
    // 添加布局
    QLabel *txt = nullptr;
    txt = new QLabel(tr("You should restart the system to make the changes in peripherals management effective. Make sure your files have been saved, and then reboot."));

    // 字体剧中
    txt->setAlignment(Qt::AlignCenter);
    txt->setWordWrap(true);
    addContent(txt);

    // 针对DTK弹窗目前accessibleName会随系统语言变化的情况
    // 通过insertButton的形式定制按钮,保持accessiableName的一致性
    QPushButton *cancelBtn = new QPushButton(this);
    cancelBtn->setText(tr("Cancel", "button"));
    cancelBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(0, cancelBtn, false);

    rebootBtn = new QPushButton(this);
    rebootBtn->setText(tr("Reboot Now (%1)").arg(m_elapsedSeconds));
    rebootBtn->setAttribute(Qt::WA_NoMousePropagation);
    this->insertButton(1, rebootBtn, true);
    rebootBtn->setEnabled(false);

    // 添加信号槽
    connect(this, SIGNAL(buttonClicked(int, const QString &)), this, SLOT(onButtonClicked(int, const QString &)));

    // 安装定时器
    m_timer = new QTimer();
    connect(m_timer, SIGNAL(timeout()), this, SLOT(onTimerout()));
    m_timer->start(1000);
}

void SystemRebootDialog::onButtonClicked(int index, const QString &text)
{
    Q_UNUSED(text)

    if (index == BTN_REBOOT) {
        Q_EMIT sendRebootSystem();
    }
    // 停止定时器、关闭弹框
    m_timer->stop();
    close();
}

void SystemRebootDialog::onTimerout()
{
    m_elapsedSeconds--;

    // 实时变化按钮的时间
    if (m_elapsedSeconds > 0) {
        setButtonText(1, tr("Reboot Now (%1)").arg(m_elapsedSeconds));
        return;
    } else {
        setButtonText(1, tr("Reboot Now"));
        rebootBtn->setEnabled(true);
        m_timer->stop();
    }
}

SystemRebootDialog::~SystemRebootDialog()
{
    Q_EMIT onDialogClose();

    m_timer->stop();
    m_timer->deleteLater();
}
